package com.pocket.demo.core.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author cookie.
 **/
public class LogUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogUtil.class);

    public static void setLevel(LogConstant.LogType logType, String logLevel) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        if (LogConstant.LOG_PACKAGES_MAP.containsKey(logType) && StringUtils.isNoneBlank(logLevel)){
            LogConstant.LOG_PACKAGES_MAP.get(logType).stream().forEach(
                    packageName -> {
                        LOGGER.debug("设置包 {} 日志级别为：{}", packageName, logLevel);
                        loggerContext.getLogger(packageName).setLevel(Level.toLevel(logLevel));
                    }
            );
        }
    }

    // TODO 根据系统的 configuration 对象初始化所有日志级别
}
